<?php


namespace app\admin\controller\notice;


use app\common\controller\Adminbase;
use think\Db;

class Msg extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    //列表
    public function index()
    {
        // 1用户 2商户 3平台
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['role', 'in', [1, 2]];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['role'])) {
                    $where[] = ['role', '=', $filter['role']];
                }
            }
            $_list = Db::name('msg')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            $total = Db::name('msg')->where($where)->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['member_info'] = Db::name('member')->find($v['member_id']);
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);

                if ($v['role'] == 1) {
                    $salesperson_info = Db::name('salesperson')->find($v['obj_id']);
                    $_list[$k]['obj_info'] = $salesperson_info['id'] . '----' . $salesperson_info['name'] . '----' . $salesperson_info['phone'];
                } else {
                    $shop_info = Db::name('shop')->find($v['obj_id']);
                    $_list[$k]['obj_info'] = $shop_info['id'] . '----' . $shop_info['shop_name'] . '----' . $shop_info['phone'];
                }
                $notice_info = Db::name('notice')->where('id', '=', $v['notice_id'])->find();
                if ($notice_info) {
                    $_list[$k]['notice'] = $notice_info['title'];
                }

            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }


    public function sendList()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $msg_info = Db::name('msg')->find($id);
            if ($msg_info['notice_id']) {
                $where[] = ['notice_id', '=', $msg_info['notice_id']];
            } else {
                $where[] = ['notice_id', '=', 0];
            }
            $where[] = ['role', 'in', [$msg_info['role'], 3]];
            $where[] = ['obj_id', '=', $msg_info['obj_id']];
            $where[] = ['member_id', '=', $msg_info['member_id']];


            $_list = Db::name('msg')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            $total = Db::name('msg')->where($where)->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['member_info'] = Db::name('member')->find($v['member_id']);
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);

                if ($v['role'] == 1) {
                    $salesperson_info = Db::name('salesperson')->find($v['obj_id']);
                    $_list[$k]['obj_info'] = $salesperson_info['id'] . '----' . $salesperson_info['name'] . '----' . $salesperson_info['phone'];
                } else {
                    $shop_info = Db::name('shop')->find($v['obj_id']);
                    $_list[$k]['obj_info'] = $shop_info['id'] . '----' . $shop_info['shop_name'] . '----' . $shop_info['phone'];
                }
                $notice_info = Db::name('notice')->where('id', '=', $v['notice_id'])->find();
                if ($notice_info) {
                    $_list[$k]['notice'] = $notice_info['title'];
                }

            }

            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'id' => $id,
        ]);
        return $this->fetch();
    }

    //消息回复
    public function send()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $msg_info=Db::name('msg')->find($data['id']);
            $stmt=sendMsg(3,$msg_info['member_id'],$data['content'],$msg_info['obj_id'],$msg_info['notice_id']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $msg_data=Db::name('msg')->find($id);
        $this->assign([
            'data'=>$msg_data
        ]);

        return $this->fetch();
    }

}